﻿using System;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace BookSourceVerification.service.impl;

public class UrlCheckPingServiceImpl : UrlCheckService
{
    public override string GetUrl(JsonNode node)
    {
        var url = node["bookSourceUrl"]?.ToString() ??
                  node["searchUrl"]?.ToString() ?? throw new WarningException("无法获取到url");
        try
        {
            Uri uri = new Uri(url);
            return uri.Host;
        }
        catch (Exception e)
        {
            throw new Exception($"域名格式错误: {url}", e);
        }
    }

    public override string GetUrl(JsonElement element)
    {
        var url = element.GetProperty("bookSourceUrl").GetString()
                  ?? element.GetProperty("searchUrl").GetString()
                  ?? throw new WarningException("无法获取到url");

        try
        {
            Uri uri = new Uri(url);
            return uri.Host;
        }
        catch (Exception e)
        {
            throw new Exception($"域名格式错误: {url}", e);
        }
    }
}